package com.dkd.manage.service.impl;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.Node;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.service.IChannelService;
import com.dkd.manage.service.INodeService;
import com.dkd.manage.service.IVendingMachineService;
import com.dkd.manage.service.IVmTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author hiema
 * @date 2024-09-25
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private INodeService nodeService;
    @Autowired
    private IVmTypeService vmTypeService;
    @Autowired
    private IChannelService channelService;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine) {
        // 添加点位信息
        Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
        BeanUtils.copyProperties(node, vendingMachine, "id");
        // 设置地址
        vendingMachine.setAddr(node.getAddress());
        // 添加设备类型信息 容量
        VmType vmType = vmTypeService.selectVmTypeById(vendingMachine.getVmTypeId());
        vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        // 生成设备唯一编号
        String uuid = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(uuid);
        // 设置设备状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        // 设置时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        // 添加完成
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        // 添加货道信息
        // 定义List集合存储货道
        List<Channel> channelList = new ArrayList<>();
        for (int i = 0; i < vmType.getVmRow(); i++) {
            for (int j = 0; j < vmType.getVmCol(); j++) {
            //     初始化Channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());
            //     设置时间
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channelList.add(channel);
            }
        }
        // 插入货道
        channelService.batchInsertChannels(channelList);

        return result;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        if(vendingMachine.getNodeId() != null) {
            // 获取node信息
            Node node = nodeService.selectNodeById(vendingMachine.getNodeId());
            // 合作商，区域，点位信息
            BeanUtils.copyProperties(node, vendingMachine, "id");
            // 设置addr
            vendingMachine.setAddr(node.getAddress());
        }
        // 更新时间
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
